package com.liqi.frame.comm.dto;

import com.liqi.frame.comm.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * json结果数据传输对象
 * @author liqi on 2017-03-23.
 * liqiwork@qq.com
 *
 */
@Data
@ApiModel(value = "HttpResult统一数据返回对象", description = "所有数据经此包装")
public final class HttpResult<T> {
    @ApiModelProperty(required = true, value = "返回状态码", dataType = "int", position = 0)
    private T data;
    @ApiModelProperty(required = true, value = "返回message信息", dataType = "string", position = 1)
    private Integer code;
    @ApiModelProperty(required = true, value = "返回数据", dataType = "string", position = 2)
    private String msg;

    public HttpResult(){
        this(ResultEnum.SUCCESS);
    }

    public HttpResult(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.data = data;
    }

    public HttpResult(Integer code,String msg, T data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public HttpResult(ResultEnum resultEnum, String msg) {
        this.code = resultEnum.getCode();
        this.msg = msg;
    }

    public HttpResult(ResultEnum resultEnum, String msg, T data) {
        this.code = resultEnum.getCode();
        this.msg = msg;
        this.data = data;
    }

    public HttpResult(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMessage();
    }

    public static HttpResult success(){
        return new HttpResult<>(ResultEnum.SUCCESS);
    }
    public static HttpResult success(Object data){
        return new HttpResult<>(ResultEnum.SUCCESS, data);
    }
    public static HttpResult success(String msg){
        return new HttpResult<>(ResultEnum.SUCCESS, msg);
    }
    public static HttpResult success(String msg, Object data){
        return new HttpResult<>(ResultEnum.SUCCESS, msg, data);
    }

    public static HttpResult failure(){
        return new HttpResult<>(ResultEnum.FAILURE);
    }
    public static HttpResult failure(Object data){
        return new HttpResult<>(ResultEnum.FAILURE, data);
    }
    public static HttpResult failure(String msg, Object data){
        return new HttpResult<>(ResultEnum.FAILURE, msg, data);
    }
    public static HttpResult failure(String msg){
        return new HttpResult<>(ResultEnum.FAILURE, msg);
    }

    public static HttpResult result(Integer code, String msg, Object data){
        return new HttpResult<>(code, msg, data);
    }

}
